<?php

require_once 'Student.php';
require_once 'User.php';
require_once 'Page.php';
require_once 'Assert.php';

# student持久化操作
class StudentMapper
{
    /**
     * 分页查询学生信息列表
     * @param Student $condition 查询条件
     * @param Page|null $page 分页条件
     * @return string 动态sql
     */
    public function listStudentInfoByPage(Student $condition, Page $page): string
    {
        $sql = "select * from student_manage.student where 1 = 1 ";
        $sql .= $this->appendCommonCondition($condition);

        // 处理分页参数，设置默认值
        $page->pageSize = $page->pageSize !== null ? $page->pageSize : 15;
        $page->cur = $page->cur !== null ? $page->cur : 1;

        // 计算偏移量
        $offset = ($page->cur - 1) * $page->pageSize;

        // 添加LIMIT子句用于分页
        $sql .= " limit " . $offset . ", " . $page->pageSize;
        return $sql;
    }

    /**
     * 通过学生编号检索学生信息
     * @param $id int 学生id
     * @return string 动态sql
     */
    public function getById(int $id): string
    {
        return "select * from student_manage.student where id = " . $id;
    }

    /**
     * 通过学生编号删除学生信息
     * @param $id int 学生id
     * @return string 动态sql
     */
    public function deleteById(int $id): string
    {
        return "delete from student_manage.student where id = " . $id;
    }

    /**
     * 新增学生信息
     * @param Student $saveInfo
     * @return string 动态sql
     */
    public function addStudent(Student $saveInfo): string
    {
        Assert::isTrue(!is_null($saveInfo->id), "未填写学生编号");
        Assert::isTrue(!empty(trim($saveInfo->name)), "未填写学生名称");
        Assert::isTrue(!empty(trim($saveInfo->sex)), "未填写学生性别");
        $insertPattern = "insert into student_manage.student(id, name, sex, age, city) value ( %d, '%s', '%s', %d,'%s')";
        return sprintf($insertPattern,
            $saveInfo->id, $saveInfo->name, $saveInfo->sex,
            $saveInfo->age, $saveInfo->city);
    }

    /**
     * 追加通用查询条件
     * @param Student $condition 构建条件
     * @return string 动态sql
     */
    public function appendCommonCondition(Student $condition): string
    {
        $sql = "";
        if (!is_null($condition->id) && $condition->id > 0) {
            $sql .= " and id like '%" . $condition->id . "%'";
        }

        if (!is_null($condition->name) && !empty(trim($condition->name))) {
            $sql .= " and name like '%" . $condition->name . "%'";
        }

        if (!is_null($condition->ageStart) && $condition->ageStart > 0) {
            $sql .= " and age >= " . $condition->$condition->ageStart;
        }

        if (!is_null($condition->ageEnd) && $condition->ageEnd > 0) {
            $sql .= " and age <= " . $condition->$condition->ageEnd;
        }
        return $sql;
    }

    /**
     * 条件统计学生数据
     * @param Student $condition 统计条件
     * @return string 模版sql
     */
    public function conditionCount(Student $condition)
    {
        return $this->fullCount() . $this->appendCommonCondition($condition);
    }

    /**
     * 无条件统计
     * @return string 模版sql
     */
    public function fullCount(): string
    {
        return "select count(*) total_count from student_manage.student where 1=1 ";
    }

    public function getByUsername(string $username): string
    {
        return "select * from student_manage.user where username = '" . $username . "'";
    }

    public function getByEmail(string $email): string
    {
        return "select * from student_manage.user where email = '" . $email . "'";
    }

    public function getByUsernameAndPassword(string $username, string $password): string
    {
        return "select * from student_manage.user where username = '" . $username . "' and password = '" . $password . "'";
    }

    public function insertUser(User $saveInfo): string
    {
        $insertPattern = "insert into student_manage.user(username, password, email) value ('%s', '%s', '%s')";
        return sprintf($insertPattern, $saveInfo->username, password_hash($saveInfo->password, PASSWORD_DEFAULT), $saveInfo->email);
    }
}